树莓派5刷Raspberry Pi OS和其他功能


更新日期

2025.06.30

完成功能

注意,连接树莓派的typec线和充电器支持pd
原装充电器5.1v5a,不要用太垃圾的充电器,会经常红灯无法启动
最好装有风扇的外壳,用的一个风扇的外壳,啥都没干温度就46了,拿个usb风扇吹了几分钟34度
信号不太好,不要离wifi太远
需要一些linux命令行基础
docker和其他一些功能,可能需要路由器翻墙
参考文章https://andi.wang/go?id=36
部分软件分享https://pan.baidu.com/s/1uvgT0eSmNST-e_ymBcDWBw?pwd=nmqx

使用raspberry pi imager在tf写入系统

使用raspberry pi imager,https://downloads.raspberrypi.org/imager/imager_latest.exe
如果之前有其他系统,可以先在操作系统里最下面选择格式化下一步,再选需要的系统
树莓派设置:raspberry pi 5
操作系统:raspberry pi os 64bit,只命令行无界面的话选raspberry pi os other,raspberry pi os lite 64bit
存储设置:选择你的tf卡目录
下一步,编辑设置
通用:

  • 主机名,用户名密码,配置wlan
  • 本地化设置:时区Asia/Shanghai
    服务:
  • 开启ssh,使用密码登录
    保存,确认,确认

启动后发现还是没法ssh连接

使用minihdmi连接屏幕,鼠标,右上角wifi设置country选择china,就自动连接了,看来还是有bug,写镜像的时候没找到设置country的

设置

不带屏幕和键盘的树莓派开启ssh

如果写入系统时没开启ssh服务,电脑上读取tf卡,bootfs分区,新建一个文件,文件名ssh,没有扩展名,没有内容

设置风扇转速

如果安装了带风扇的壳,线连接在最外面的usb口旁边,可以控制转速
使用windows的文本编辑器可能会导致树莓派无法启动,建议才linux下编辑
bootfs分区,编辑config.txt,最后添加

1
2
3
4
5
dtparam=cooling_fan=on
dtparam=fan_temp0=42000,fan_temp0_hyst=2000,fan_temp0_speed=100
dtparam=fan_temp1=50000,fan_temp1_hyst=5000,fan_temp1_speed=150
dtparam=fan_temp2=60000,fan_temp2_hyst=5000,fan_temp2_speed=200
dtparam=fan_temp3=70000,fan_temp3_hyst=5000,fan_temp3_speed=250

每行的意思,以第二行为例,cpu温度42度开始转,42-2度=40度时停止(下面几个是切换到低档位),转速80(最大255)
其他说明在/boot/overlays/README

使用root用户登录ssh

修改/etc/ssh/sshd_config,默认是第一行,下面添加第二行

1
2
#PermitRootLogin prohibit-password
PermitRootLogin yes

之后的命令有些需要root权限,普通用户运行前要加sudo

切换软件源

删除/etc/apt/sources.list内的内容
编辑/etc/apt/sources.list.d/raspi.list文件

1
2
3
4
5
6
7
8
9
10
11
12
deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware

deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware
deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware

deb http://mirrors.ustc.edu.cn/debian-security/ bookworm-security main contrib non-free non-free-firmware
~

更新软件源和软件

1
2
apt update
apt upgrade

安装常用软件

1
apt install vim python3-pip docker-compose

设置pip镜像地址

1
pip config set global.index-url https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple

添加自定义,修改ls时间显示格式

编辑~/.bashrc添加

1
2
3
alias l='ls -ahl'
alias t='vcgencmd measure_temp'
export TIME_STYLE='+%Y-%m-%d %H:%M:%S'

l显示文件列表,t显示cpu温度

其他常用命令

1
2
3
4
5
6
# 查看cpu温度
vcgencmd measure_temp
# 设置时区
timedatectl set-timezone Asia/Shanghai
# 升级系统
rpi-update

服务

webdav服务器

https://andi.wang/go?search=webdav

pm2.5检测系统

https://andi.wang/go?search=攀藤PMS5003ST

filebrowser网页文件管理程序

https://andi.wang/2022/01/26/%E4%BD%BF%E7%94%A8archlinux%E6%90%AD%E5%BB%BA%E5%AE%B6%E5%BA%AD%E6%95%B0%E6%8D%AE%E6%9C%8D%E5%8A%A1%E4%B8%AD%E5%BF%83/#filebrowser

离线签到

https://andi.wang/2022/01/26/%E4%BD%BF%E7%94%A8archlinux%E6%90%AD%E5%BB%BA%E5%AE%B6%E5%BA%AD%E6%95%B0%E6%8D%AE%E6%9C%8D%E5%8A%A1%E4%B8%AD%E5%BF%83/#%E7%AD%BE%E5%88%B0%E8%84%9A%E6%9C%AC

adminer数据库管理

下载主程序https://www.adminer.org/en/#download
下载sqlite无密码登录插件https://github.com/vrana/adminer/blob/master/plugins/login-password-less.php
下载sqlite登录页面https://github.com/vrana/adminer/blob/master/adminer/sqlite.php
三个文件放在同一个目录下
sqlite.php修改第3行,第6行修改成自己需要的密码,第10行改成adminer主程序的文件名

1
2
3
	include_once "./login-password-less.php";
new AdminerLoginPasswordLess(password_hash("123", PASSWORD_DEFAULT)),
include "./adminer-5.3.0.php";

浏览器访问sqlite.php,系统sqlite,用户名空,密码123(自己设置的密码),数据库填绝对路径,例如/srv/http/test/test.db
就可以进入数据库了

home-assistant智能家居(docker方式)

新建一个保存home-assistant文件的文件夹/srv/homeassistant

1
2
3
4
5
6
7
8
9
docker run -d \
--name homeassistant \
--privileged \
--restart=unless-stopped \
-e TZ=Asia/Shanghai \
-v /srv/homeassistant:/config \
-v /run/dbus:/run/dbus:ro \
--network=host \
ghcr.io/home-assistant/home-assistant:stable

编辑/srv/homeassistant/configuration.yaml,最后添加

1
2
3
4
5
http:
use_x_forwarded_for: true
trusted_proxies:
- 192.168.31.0/24
- 127.0.0.1

重启home assistant,docker restart homeassistant
浏览器访问ip:8123就有界面了,创建账号,进入主界面后,左下角点击自己账号,右侧高级模式,打开

安装hacs,添加小米设备

项目地址https://github.com/XiaoMi/ha_xiaomi_home
新建文件夹/srv/homeassistant/custom_components/hacs
下载hacs.zip,https://github.com/hacs/integration/releases,解压到hacs文件夹
重启home assistant,docker restart homeassistant
设置,设备与服务,右下角添加集成,hacs
打开提示的github页面,填写提示的8位验证码,完成
home assistant界面左侧出现HACS,点击,右边点Xiaomi Home,右下角download
重启home assistant,docker restart homeassistant
设置,设备与服务,已发现,xiaomi_home,添加,请点击此处进行登录,小米界面登陆
如果跳转到错误页面,把网址的ip修改成你的设备ip
添加选择家庭(xx个设备),home-assistant主界面概览就能看见小米设备了
更新小米设备:设置,设备与服务,xiaomi home,配置,更新设备列表,下一步,下一步,确认修改,提交

添加json数据和命令行数据

编辑/srv/homeassistant/configuration.yaml
cpu温度数据来自linux系统,pm2.5数据来自上面的pm2.5检测系统
unit_of_measurement没有这一行,点击卡片就无法显示图表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
command_line:
- sensor:
name: RaspberryPiCPUTemperature
command: "cat /sys/class/thermal/thermal_zone0/temp"
scan_interval: 600
unit_of_measurement: "°C"
value_template: '{{ value | multiply(0.001) | round(1) }}'
- sensor:
name: RaspberryPiTemperature
scan_interval: 600
unit_of_measurement: "°C"
command: "curl -s http://127.0.0.1/pm25/data.json"
value_template: '{{ value_json.temperature }}'
- sensor:
name: RaspberryPiHumidity
scan_interval: 600
unit_of_measurement: "%"
command: "curl -s http://127.0.0.1/pm25/data.json"
value_template: '{{ value_json.humidity }}'
- sensor:
name: RaspberryPiPM25
scan_interval: 600
unit_of_measurement: "μg/m³"
command: "curl -s http://127.0.0.1/pm25/data.json"
value_template: '{{ value_json.pm25 }}'

概览,右上编辑仪表盘,添加卡片,按卡片,水平堆叠,添加3个传感器,搜索RaspberryPiTemperature等

添加摄像头

编辑/srv/homeassistant/configuration.yaml,注意修改密码和ip

1
2
3
4
camera:
- platform: ffmpeg
name: 客厅摄像头
input: -rtsp_transport tcp -i rtsp://admin:密码@192.168.1.100:554/stream1

概览,右上编辑仪表盘,添加卡片,按卡片,图片预览,摄像头实体:选择自己的,摄像头视图:实时,保存
如果不想每次都看摄像头,就在概览下面新建一个视图专门放摄像头

水星摄像头地址rtsp://admin:客户端设置的密码@192.168.1.100:554/stream1
萤石摄像头地址rtsp://admin:机器下面贴的6位大写英文@192.168.1.100:554/h264/ch1/main/av_stream
其他摄像头地址参考https://github.com/AlexxIT/WebRTC

添加天气

https://github.com/hasscc/tianqi
hacs安装https://my.home-assistant.io/redirect/hacs_repository/?owner=hasscc&repository=tianqi&category=integration
hacs安装https://my.home-assistant.io/redirect/hacs_repository/?owner=fineemb&repository=lovelace-colorfulclouds-weather-card&category=lovelace
设置,设备与服务,右下角添加集成,天气预报,服务器域weather.com.cn,搜索地点城市或区,提交后会出现选项,选择
概览,右上编辑仪表盘,添加卡片,按卡片,Colorfulclouds Weather Lovelace Card,下拉框选weather.地区名,保存

备份home assistant

网页中,设置,系统,备份,右下角立即备份,手动备份,下一步,创建备份,点击n个手动备份,右侧三个点,下载

添加宜家希姆弗斯(sonos)

安卓的sonos总是卡在自动更新,用不了,windows端可以正常更新,大概10-20分钟,不确定是不是需要翻墙
之前sonos连接过wifi,如果没连过就尝试用网线连接
home assistant的hacs里安装Mini Media Player
设置,设备与服务,已发现中自动有sonos,添加
概览,右上编辑仪表盘,添加卡片,按卡片,Mini Media Player,entity里选择刚才添加的
点击mini player界面,浏览媒体,有一些可以播放

添加南方电网数据

hacs安装China Southern Power Grid Statistics
设置,设备与服务,右下角添加集成,China Southern Power Grid Statistics,登录
设置,设备与服务,China Southern Power Grid Statistics,配置,添加已绑定的缴费号,选择需要的
概览,右上编辑仪表盘,添加卡片,按卡片,传感器,实体,搜缴费号就能看见数据了
常用的:前一天用电量last_day_kwh,不清楚和yesterday_kwh有啥区别,本月用电量this_month_total_usage

常见问题

按钮点击后弹出窗口,而没有按下:配置,互动,点击行为:切换